package com.TeacherInfoServer.common.service.Impl;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.pojo.DTO.AddWorkExperienceDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.WorkExperienceSearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateWorkExperienceDTO;
import com.TeacherInfoServer.pojo.entity.WorkExperience;
import com.TeacherInfoServer.pojo.vo.WorkExperienceListVo;
import com.TeacherInfoServer.pojo.vo.WorkExperienceVo;
import com.TeacherInfoServer.exception.JobNumberNotExistException;
import com.TeacherInfoServer.mapper.WorkExperienceMapper;
import com.TeacherInfoServer.common.service.TeacherService;
import com.TeacherInfoServer.common.service.WorkExperienceService;
import com.TeacherInfoServer.utils.HttpServletUtil;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class WorkExperienceServiceImpl extends ServiceImpl<WorkExperienceMapper, WorkExperience> implements WorkExperienceService {

    @Autowired
    private WorkExperienceMapper workExperienceMapper;
    @Autowired
    private TeacherService teacherService;

    @Override
    @Transactional
    public Result<Map<String, Integer>> addOne(AddWorkExperienceDTO addWorkExperienceDTO) {
        if (teacherService.getOne(addWorkExperienceDTO.getTeacherId()).getData() != null){
            WorkExperience workExperience =
                    new WorkExperience(null,
                            addWorkExperienceDTO.getTeacherId(),
                            addWorkExperienceDTO.getUnit(),
                            addWorkExperienceDTO.getJobs(),
                            addWorkExperienceDTO.getStartDate(),
                            addWorkExperienceDTO.getEndDate(),1,null);
            Integer record = workExperienceMapper.insert(workExperience);
            Map<String,Integer> map = new HashMap<>();
            map.put("record",record);
            return Result.success(map);
        } else {
            throw new JobNumberNotExistException();
        }
    }

    @Override
    @Transactional
    public Result<WorkExperienceListVo> getList(Long pageNumber, Long pageSize) {
        Page<WorkExperience> page = new Page<>(pageNumber,pageSize);
        List<WorkExperience> workExperienceList = workExperienceMapper.selectPageWithTeacher(page);
        List<WorkExperienceVo> workExperienceVoList = getWorkExperienceVoList(workExperienceList);
        WorkExperienceListVo workExperienceListVo =
                new WorkExperienceListVo(page.getCurrent(),
                        page.getSize(),
                        page.getPages(),
                        page.getTotal(),
                        workExperienceVoList);
        return Result.success(workExperienceListVo);
    }
    @Override
    public Map<String,Object> searchWorkExperienceList(WorkExperienceSearchDTO workExperienceSearchDTO) {
        Page<?> page = new Page<>(workExperienceSearchDTO.getPages(), workExperienceSearchDTO.getSize());
        workExperienceMapper.searchWorkExperienceList(page, workExperienceSearchDTO);
        Map<String, Object> data = new HashMap<>();
        data.put("totals", page.getTotal());
        data.put("currentPages", page.getCurrent());
        data.put("pageNumber", page.getPages());
        data.put("rows", page.getRecords());
        return data;
    }

//    @Override
//    @Transactional
//    public Result<WorkExperienceListVo> getOneList(Long pageNumber, Long pageSize, String teacherId) {
//        Page<WorkExperience> page = new Page<>(pageNumber,pageSize);
//        LambdaQueryWrapper<WorkExperience> wrapper = new LambdaQueryWrapper<>();
//        List<WorkExperience> workExperienceList = workExperienceMapper.selectPage(page,wrapper.eq(WorkExperience::getTeacherId,teacherId).orderByDesc(WorkExperience::getEndDate)).getRecords();
//        WorkExperienceListVo workExperienceListVo =
//                new WorkExperienceListVo(page.getCurrent(),
//                        page.getSize(),
//                        page.getPages(),
//                        page.getTotal(),
//                        workExperienceList);
//        return Result.success(workExperienceListVo);
//    }

    @Override
    @Transactional
    public Result<Map<String, Integer>> deleteBatch(Integer[] ids) {
        Integer record = workExperienceMapper.deleteBatchIds(Arrays.asList(ids));
        Map<String, Integer> map = new HashMap<>();
        map.put("record", record);
        return Result.success(map);
    }

    @Override
    @Transactional
    public Result<Map<String, Integer>> updateOne(UpdateWorkExperienceDTO updateWorkExperienceDTO) {
        if (teacherService.getOne(updateWorkExperienceDTO.getTeacherId()).getData() != null){
            LambdaUpdateWrapper<WorkExperience> wrapper = new LambdaUpdateWrapper<>();
            Integer record = workExperienceMapper.update(null,wrapper.eq(WorkExperience::getId, updateWorkExperienceDTO.getId())
                    .set(WorkExperience::getTeacherId, updateWorkExperienceDTO.getTeacherId())
                    .set(WorkExperience::getUnit, updateWorkExperienceDTO.getUnit())
                    .set(WorkExperience::getJobs, updateWorkExperienceDTO.getJobs())
                    .set(WorkExperience::getStartDate, updateWorkExperienceDTO.getStartDate())
                    .set(WorkExperience::getEndDate, updateWorkExperienceDTO.getEndDate()));
            Map<String, Integer> map = new HashMap<>();
            map.put("record", record);
            return Result.success(map);
        } else {
            throw new JobNumberNotExistException();
        }
    }

    @Override
    public void export(HttpServletResponse response, String fileName) throws IOException {
        HttpServletUtil.setExportResponse(response,fileName);
        List<WorkExperience> workExperienceList = workExperienceMapper.selectPageWithTeacher(null);
        List<WorkExperienceVo> workExperienceVoList = getWorkExperienceVoList(workExperienceList);
        EasyExcel.write(response.getOutputStream(), WorkExperienceVo.class).excelType(ExcelTypeEnum.XLSX).sheet().doWrite(workExperienceVoList);
    }

    private List<WorkExperienceVo> getWorkExperienceVoList(List<WorkExperience> workExperienceList){
        return workExperienceList.stream()
                .map(workExperience -> new WorkExperienceVo(
                        workExperience.getId(),
                        workExperience.getTeacherId(),
                        workExperience.getTeacher().getTeacherName(),
                        workExperience.getUnit(),
                        workExperience.getJobs(),
                        workExperience.getStartDate(),
                        workExperience.getEndDate()
                ))
                .collect(Collectors.toList());
    }

}
